package es.juanc.mail.apache;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

public class ApacheCommonsMail {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		simpleMail();
		attachedMail();
		htmlFormattedMail();
	}
	
	private static void simpleMail() {
		
		SimpleEmail email = new SimpleEmail();

		email.setHostName("smtp.gmail.com");
		email.setSmtpPort(587);
		email.setTLS(true);
		email.setAuthentication("juancsch@gmail.com","tachatacha");

		try {
			email.addTo("juancsch@gmail.com", "Juan Carlos");
			email.setFrom("juancsch@gmail.com", "Juan Carlos");
			email.setSubject("Test message");
			email.setMsg("This is a simple test of commons-email");
			email.send();
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void attachedMail() {

		  try {
			  // Create the attachment
		  	  EmailAttachment attachment = new EmailAttachment();
		  	  attachment.setPath("prueba.xls");
		  	  attachment.setDisposition(EmailAttachment.ATTACHMENT);
		  	  attachment.setDescription("excel de datos");
		  	  attachment.setName("excel");  
//			  EmailAttachment attachment = new EmailAttachment();
//			  attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
//			  attachment.setDisposition(EmailAttachment.ATTACHMENT);
//			  attachment.setDescription("Apache logo");
//			  attachment.setName("Apache logo");

		  	  // Create the email message
			  MultiPartEmail email = new MultiPartEmail();

			  email.setHostName("smtp.gmail.com");
			  email.setSmtpPort(587);
			  email.setTLS(true);
			  email.setAuthentication("juancsch@gmail.com","tachatacha");
			  
		      email.addTo("juancsch@gmail.com", "Juan Carlos");
			  email.setFrom("juancsch@gmail.com", "Juan Carlos");
			  email.setSubject("Test message");
			  email.setMsg("This is a simple test of commons-email");

			  // add the attachment
			  email.attach(attachment);

			  // send the email
			  email.send();
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void htmlFormattedMail() {
		
		  try {
			  // Create the email message
			  HtmlEmail email = new HtmlEmail();

			  email.setHostName("smtp.gmail.com");
			  email.setSmtpPort(587);
			  email.setTLS(true);
			  email.setAuthentication("juancsch@gmail.com","tachatacha");
				
			  email.addTo("juancsch@gmail.com", "Juan Carlos");
			  email.setFrom("juancsch@gmail.com", "Juan Carlos");
			  email.setSubject("Test message");
			   
			  // embed the image and get the content id
			  URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
			  String cid = email.embed(url, "Apache logo");
			  
			  // set the html message
			  email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

			  // set the alternative message
			  email.setTextMsg("Your email client does not support HTML messages");

			  // send the email
			  email.send();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}